#include "LayerListUI.h"


LayerListUIEntry::~LayerListUIEntry()
{
}

LayerListUI::LayerListUI() :
    m_cbLayerAdd("Add Layer"),
    m_cbLayerRemove("Remove Layer"),
    m_cbLayerShiftUp("Shift Layer up"),
    m_cbLayerShiftDown("Shift Layer Down"),
    m_layerList(*(Layer_List::get_instance())),
    m_currentCtx(*(CurrentContext::get_instance()))
{
    pack_start(m_cvbLayerListUIEntryBox);
    m_chbAddRemoveLayerBox.pack_start(m_cbLayerAdd);
    m_chbAddRemoveLayerBox.pack_start(m_cbLayerRemove);;
    m_chbShiftLayerBox.pack_start(m_cbLayerShiftUp);
    m_chbShiftLayerBox.pack_start(m_cbLayerShiftDown);

    pack_start(m_chbAddRemoveLayerBox);
    pack_start(m_chbShiftLayerBox);

    LayerListUIEntry *layerZero = manage(new LayerListUIEntry(0, m_crbGroup));
    cout << "Layer 0" << layerZero << endl;
    m_layerListEntryList.push_back(layerZero);
    m_cvbLayerListUIEntryBox.pack_start(*layerZero);

    m_crbGroup = layerZero->m_crblayerCurrent.get_group();

    m_cbLayerAdd.signal_clicked().connect(sigc::mem_fun(*this,
        &LayerListUI::on_addLayer_Button_clicked) );

    m_cbLayerRemove.signal_clicked().connect(sigc::mem_fun(*this,
        &LayerListUI::on_removeLayer_Button_clicked) );

    m_cbLayerShiftUp.signal_clicked().connect(sigc::mem_fun(*this,
        &LayerListUI::on_shiftLayerUp_Button_clicked) );

    m_cbLayerShiftDown.signal_clicked().connect(sigc::mem_fun(*this,
        &LayerListUI::on_shiftLayerDown_Button_clicked) );

    layerZero->m_crblayerCurrent.signal_clicked().connect(sigc::mem_fun(*this,
        &LayerListUI::on_setActiveLayer_RadioButton_clicked) );

    layerZero->m_crbvisible.signal_clicked().connect(sigc::mem_fun(*this,
        &LayerListUI::on_setLayerVisblie_RadioButton_clicked) );

    show_all_children();
}

void LayerListUI::on_addLayer_Button_clicked()
{
    uint newLayerId = m_layerList.addLayer();

    if(Debug::test(DEBUG_INFO)) {
        cout << "New layer " << newLayerId << " adding" << endl;
    }
    LayerListUIEntry *newLayer = NULL;

    list<LayerListUIEntry *> :: iterator i;

    for (i = m_layerListEntryList.begin(); i != m_layerListEntryList.end(); i++) {
         if ((*i)->deleted) {
             newLayer = *i;
             newLayer->deleted = false;
             newLayer->m_ilayerId = newLayerId;
         }
    }
    if(!newLayer) {
        newLayer = manage(new LayerListUIEntry(newLayerId, m_crbGroup));
        m_layerListEntryList.push_back(newLayer);
    }

    if (!newLayer) cout << "!newLayer" << endl;
    cout << "new LAYER " << newLayer << endl;

    newLayer->m_crblayerCurrent.signal_clicked().connect(sigc::mem_fun(*this,
        &LayerListUI::on_setActiveLayer_RadioButton_clicked) );

    newLayer->m_crbvisible.signal_clicked().connect(sigc::mem_fun(*this,
        &LayerListUI::on_setLayerVisblie_RadioButton_clicked) );

    m_cvbLayerListUIEntryBox.pack_start(*newLayer);
    newLayer->m_crblayerCurrent.set_active();
    m_currentCtx.setCurrentLayerIndex(newLayerId);
    m_currentCtx.invalidateActiveSpriteInfo();
    show_all_children();
}

void LayerListUI::on_removeLayer_Button_clicked()
{
    list<LayerListUIEntry *> :: iterator i;
    int newActiveLayerId;

    for (i = m_layerListEntryList.begin(); i != m_layerListEntryList.end(); i++) {
        if ((*i)->m_crblayerCurrent.get_active()) {
            newActiveLayerId = m_layerList.removeLayer((*i)->m_ilayerId);
            (*i)->deleted = true;
            (*i)->hide();

//            (*i)->m_crblayerCurrent.reset_group();
            m_cvbLayerListUIEntryBox.remove(**i);
//            delete *i;

            cout << "del LAYER " << *i << endl;
//            m_layerListEntryList.erase(i);

 /*           if(newActiveLayerId == INVALID_LAYER_ID) {
               on_addLayer_Button_clicked();
               return;
            }
*/
            m_currentCtx.setCurrentLayerIndex(newActiveLayerId);
            m_currentCtx.invalidateActiveSpriteInfo();
            break;
        }
    }
    for (i = m_layerListEntryList.begin(); i != m_layerListEntryList.end(); i++) {
        if ((*i)->m_ilayerId == newActiveLayerId) {
            (*i)->m_crblayerCurrent.set_active();
        }
    }
}

void LayerListUI::on_shiftLayerUp_Button_clicked()
{
    list<LayerListUIEntry *> :: iterator i = m_layerListEntryList.begin();

    LayerListUIEntry *prevLayer = *i;

    for (i++; i != m_layerListEntryList.end(); i++) {
        if ((*i)->m_crblayerCurrent.get_active()) {
            LayerListUIEntry *temp = *i;
            *i = prevLayer;

            //TODO
            break;
        }
    }

}

void LayerListUI::on_shiftLayerDown_Button_clicked()
{
    list<LayerListUIEntry *> :: iterator i;

    for (i = m_layerListEntryList.begin(); i != m_layerListEntryList.end(); i++) {
        if ((*i)->m_crblayerCurrent.get_active()) {
            //TODO
            break;
        }
    }
}

void LayerListUI::on_setActiveLayer_RadioButton_clicked()
{
    list<LayerListUIEntry *> :: iterator i;

    for (i = m_layerListEntryList.begin(); i != m_layerListEntryList.end(); i++) {
        if ((*i)->m_crblayerCurrent.get_active()) {
            if(Debug::test(DEBUG_INFO)) {
                cout << "Activating Layer " << (*i)->m_ilayerId << endl;
            }
            m_currentCtx.setCurrentLayerIndex((*i)->m_ilayerId);
            m_currentCtx.invalidateActiveSpriteInfo();
            break;
        }
    }
}

void LayerListUI::on_setLayerVisblie_RadioButton_clicked()
{
    list<LayerListUIEntry *> :: iterator i;

    for (i = m_layerListEntryList.begin(); i != m_layerListEntryList.end(); i++) {
        m_layerList.setVisible((*i)->m_ilayerId, (*i)->m_crbvisible.get_active());
    }
}

void LayerListUI::clear()
{
    list<LayerListUIEntry *> :: iterator i;
    for (i = m_layerListEntryList.begin(); i != m_layerListEntryList.end(); i++) {
//        (*i)->m_crblayerCurrent.reset_group();
        m_cvbLayerListUIEntryBox.remove(**i);
//        m_layerListEntryList.erase(i);
    }
    m_layerListEntryList.clear();
}
